//
//  LoginViewController.m
//  IMSDKDemo
//
//  Created by FungYan on 2022/5/5.
//

#import "HomeViewController.h"
#import "LoginViewController.h"
#import "WeCloudIMSDK/WeCloudIMSDK.h"
#import <CommonCrypto/CommonDigest.h>
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
}

- (IBAction)login:(UIButton *)sender {
    
    if (self.textField.text.length<=0) {
        return;
    }
    
    double currentTime =  [[NSDate date] timeIntervalSince1970]*1000;
    NSString *strTime = [NSString stringWithFormat:@"%.0f",currentTime];
    NSString *sign = [self md5HexDigest:[NSString stringWithFormat:@"%@%@%@%@%@",strTime,self.textField.text,@"QizKVHcILRWp6Td2",@"287d04828099fb7de871e9dda845fa8b6b2302faf2ab3737",@"3"]];
    
    [[WeCloudIMClient sharedWeCloudIM] login:strTime clientId:self.textField.text appKey:@"QizKVHcILRWp6Td2" sign:sign callback:^(BOOL success, NSString * _Nonnull token, NSString * _Nonnull clientId, WeCloudException * _Nonnull apiResult) {
        if (success) {
            NSLog(@"返回的信息：%@",clientId);
            dispatch_async(dispatch_get_main_queue(), ^{
                [[NSUserDefaults standardUserDefaults] setValue:self.textField.text forKey:@"clientId"];
                [[NSUserDefaults standardUserDefaults] setValue:token forKey:@"WeCloudIMCSdkToken"];
                
                [[NSUserDefaults standardUserDefaults] setValue:@{@"nickname":token,@"userId":clientId} forKey:@"myUserInfo"];
                
                [[WeCloudIMClient sharedWeCloudIM] openWithCallback:^(BOOL isAutoOpen, NSDictionary * _Nonnull resultDic) {
                    
                    HomeViewController *home = [[HomeViewController alloc]init];
                    home.title = @"已登录成功";
                    UINavigationController *nav= [[UINavigationController alloc]initWithRootViewController:home];
                    
                    UIWindow *window = [[UIApplication sharedApplication]delegate].window;
                    window.rootViewController = nav;
                    [window makeKeyAndVisible];
                    
                }];
                
            
            });
            
        }else{
            if (apiResult.code ==  WeCloudExceptionUnauthorized) {
                NSLog(@"token失效，请重新获取");
                //                    [MBProgressHUD showError:@"token失效，请重新获取"];
            }else if (apiResult.code == WeCloudExceptionForbidden) {
                NSLog(@"没有访问权限");
                //                    [MBProgressHUD showError:@"没有访问权限"];
            }
        }
    }];
}

- (NSString* )md5HexDigest:(NSString* )input {
    
    NSData *btdata = [input dataUsingEncoding:NSUTF8StringEncoding];
    unsigned char result[16];
    CC_MD5(btdata.bytes,(CC_LONG)btdata.length, result);
    return [NSString stringWithFormat:
                @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                result[0], result[1], result[2], result[3],
                result[4], result[5], result[6], result[7],
                result[8], result[9], result[10], result[11],
                result[12], result[13], result[14], result[15]
                ];
}

@end
